package com.kbt.activity.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.kbt.activity.GoodForPayActivity;
import com.kbt.activity.OrderActivity;
import com.kbt.activity.R;
import com.kbt.base.BaseApplication;
import com.kbt.simcpux.Constants;
import com.kbt.util.data.StaticData;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);

        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle(R.string.app_tip);
//			builder.setMessage(getString(R.string.pay_result_callback_msg, resp.errStr + ";code=" + String.valueOf(resp.errCode)));
//			builder.show();
			if(String.valueOf(resp.errCode).equals("-1")||String.valueOf(resp.errCode).equals("-2")) {
				GoodForPayActivity.shiyong_money.setText("已使用");
				GoodForPayActivity.edit_jifen_payMoney.setFocusable(false);
				BaseApplication.getInstance().isWeixin=true;
				Toast.makeText(WXPayEntryActivity.this, "您尚未支付成功，如继续购买，请点击去付款", Toast.LENGTH_LONG).show();
			}
			else {
			BaseApplication.getInstance().removeActivitytwo();
			Intent intent  = new Intent(WXPayEntryActivity.this,OrderActivity.class);
			intent.putExtra("biaoji",GoodForPayActivity.biaoji);
			Bundle bl = new Bundle();
			bl.putSerializable(StaticData.ORDER_TYPE, StaticData.ALL_ORDER);
			intent.putExtra("bundle", bl);
			intent.putExtra("wxTag", "weixin");
			startActivity(intent);
			}
			WXPayEntryActivity.this.finish();

		}
	}
}